// Copyright 2012 Square, Inc.
package com.squareup.timessquare;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yksj.healthtalk.ui.R;

public class MonthView extends LinearLayout {
  TextView title;
  CalendarGridView grid;
  private Listener listener;

  /**
   * 创建显示的方格
   * @param parent
   * @param inflater
   * @param weekdayNameFormat
   * @param listener
   * @param today
   * @return
   */
  public static MonthView create(ViewGroup parent, LayoutInflater inflater,
      DateFormat weekdayNameFormat, final Listener listener, Calendar today) {
    final MonthView view = (MonthView) inflater.inflate(R.layout.month, parent, false);
    
    //上一月
    view.findViewById(R.id.previous_month).setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(listener != null)listener.onChangeMonth(-1);
		}
	});
    
    //下一月
    view.findViewById(R.id.next_month).setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(listener != null)listener.onChangeMonth(1);
		}
	});
    
    final int originalDayOfWeek = today.get(Calendar.DAY_OF_WEEK);

    int firstDayOfWeek = today.getFirstDayOfWeek();
    final CalendarRowView headerRow = (CalendarRowView) view.grid.getChildAt(0);
    for (int offset = 0; offset < 7; offset++) {
      today.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + offset);
      final TextView textView = (TextView) headerRow.getChildAt(offset);
      textView.setText(weekdayNameFormat.format(today.getTime()));
    }
    today.set(Calendar.DAY_OF_WEEK, originalDayOfWeek);
    view.listener = listener;
    return view;
  }

  public MonthView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override protected void onFinishInflate() {
    super.onFinishInflate();
    title = (TextView) findViewById(R.id.title);
    grid = (CalendarGridView) findViewById(R.id.calendar_grid);
  }

  /**
   * 设置item状态
   * @param month 月份
   * @param cells 日期集合
   */
  public void init(MonthDescriptor month, List<List<MonthCellDescriptor>> cells) {
    long start = System.currentTimeMillis();
    title.setText(month.getLabel());
    final int numRows = cells.size();//二维数据，计算有多少行
    grid.setNumRows(numRows);//星期行数
    for (int i = 0; i < 6; i++) {
      CalendarRowView weekRow = (CalendarRowView) grid.getChildAt(i + 1);
      weekRow.setListener(listener);
      
      if (i < numRows) {
        weekRow.setVisibility(VISIBLE);
        List<MonthCellDescriptor> week = cells.get(i);
        for (int c = 0; c < week.size(); c++) {
          //设置日期，刷新状态
          MonthCellDescriptor cell = week.get(c);
          CalendarCellView cellView = (CalendarCellView) weekRow.getChildAt(c);
          cellView.setText(Integer.toString(cell.getValue()));
          cellView.setEnabled(cell.isCurrentMonth());
          cellView.setSelectable(cell.isSelectable());
          cellView.setSelected(cell.isSelected());
          cellView.setCurrentMonth(cell.isCurrentMonth());
          cellView.setToday(cell.isToday());
          cellView.setRangeState(cell.getRangeState());
          cellView.setTag(cell);
          cell.setCellView(cellView);
        }
      } else {
        weekRow.setVisibility(GONE);
      }
      
    }
    Logr.d("MonthView.init took %d ms", System.currentTimeMillis() - start);
  }

  public interface Listener {
	void onChangeMonth(int i);
    void handleClick(MonthCellDescriptor cell);
  }
}
